package com.example.stock.business;

import com.example.stock.controller.dto.DailyDetailDTO;
import com.example.stock.model.DailySat;
import com.example.stock.service.DailyDataService;
import com.example.stock.util.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

@Component
public class TradingRankSatBusiness {

    @Autowired
    DailyDataService dailyDataService;

    @Cacheable("getTradingRankSat")
    public List<Map<String, Object>> getTradingRankSat(String year) {
        List<Map<String, Object>> ret = new ArrayList<>();

        List<DailySat> dailyStaByYear = dailyDataService.getDailyStaByYear(year);

        Map<String, DailyDetailDTO> SZZU = dailyDataService.getDailyData("上证指数")
                .stream().collect(Collectors.toMap(DailyDetailDTO::getDate, i -> i));
        Map<String, DailyDetailDTO> CYB = dailyDataService.getDailyData("创业板")
                .stream().collect(Collectors.toMap(DailyDetailDTO::getDate, i -> i));
        Map<String, DailyDetailDTO> HS300 = dailyDataService.getDailyData("沪深300")
                .stream().collect(Collectors.toMap(DailyDetailDTO::getDate, i -> i));
        Map<String, DailyDetailDTO> ZZQZ = dailyDataService.getDailyData("中证全指")
                .stream().collect(Collectors.toMap(DailyDetailDTO::getDate, i -> i));

        dailyStaByYear.forEach(dailySat -> {
            Map<String, Object> item = new HashMap<>();

            String date = dailySat.getDate();

            item.put("上证指数", NumberUtil.floor(SZZU.get(date).getClose(), 2));
            item.put("上证指数PCT", SZZU.get(date).getPct());
            item.put("沪深300PCT", HS300.get(date).getPct());
            item.put("创业板PCT", CYB.get(date).getPct());
            item.put("中证全指PCT", ZZQZ.get(date).getPct());

            item.put("date", date);
            item.put("总数", dailySat.getAmount());

            // 上涨统计
            item.put("上涨个数", dailySat.getPctGet0());
            item.put("上涨超5%个数", dailySat.getPctGet5());
            item.put("下跌超5%个数", dailySat.getPctLetN5());

            // 均线
            item.put("GT_60D", dailySat.getGb60dGet0());
            item.put("GT_300D",dailySat.getGb300dGet0());

            // 分位点
            item.put("LP_LET_10",dailySat.getLpLet10());

            ret.add(item);
        });

        return ret;
    }
}
